package edu.stu.cs.software.family.base.service.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;

import edu.stu.cs.software.family.base.dao.GenericDao;
import edu.stu.cs.software.family.base.domain.BaseEntity;
import edu.stu.cs.software.family.base.service.GenericManager;

/**
 * 
 * @author Administrator
 *
 * @param <T>  a type variable
 * @param <LG> the primary key of long for that type
 */
@Transactional
public class GenericManagerImpl<T extends BaseEntity<LG>, LG extends Serializable> implements GenericManager<T, LG> {

	protected GenericDao<T, LG> dao;

	@Override
	public void delete(LG id) {
		this.dao.deleteById(id);
	}

	@Override
	public void deleteAll() {
		this.dao.deleteAll();
	}
	
	@Override
	public T findById(LG id) {
		return this.dao.findById(id).get();
	}

//	@Override
//	public Page<T> findByName(String name, Pageable pageable) {
//		return this.dao.findByName("%" + name + "%", pageable);
//	}

	@Override
	public T save(T entity) {
		T data = this.dao.save(entity);
		return data;
	}

	@Override
	public List<T> findAll() {
		return (List<T>) this.dao.findAll();
	}

	@Override
	public Page<T> findAll(Pageable page) {
		Page<T> result = this.dao.findAll(page);
		return result;
	}

	@Override
	public Iterable<T> save(Iterable<T> entities) {

		return this.dao.saveAll(entities);
	}

	@Override
	public long count() {

		return this.dao.count();
	}

	@Override
	public void deleteMore(LG[] ids) {	
		for (LG id : ids) {
            this.dao.deleteById(id);
        }
	}

}